package com.xzlcPT.service.impl;

import com.github.pagehelper.PageHelper;
import com.util.PageBean;
import com.xzlcPT.bean.*;
import com.xzlcPT.dao.*;
import com.xzlcPT.service.XzPostionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by Administrator on 2017/7/11.
 */
@Service("postionService")
public class XzPostionServiceImpl implements XzPostionService {
    @Autowired
    private XzPostionMapper postionMapper;
    @Autowired
    private XzPostionSendMapper postionSendMapper;
    @Autowired
    private XzPostionCollectMapper postionCollectMapper;
    @Autowired
    private XzMsgMapper msgMapper;
    @Autowired
    private XzCompanyMapper companyMapper;
    @Autowired
    private XzCompanyDomainMapper companyDomainMapper;
    @Autowired
    private XzCompanyWelfareMapper companyWelfareMapper;
    @Autowired
    private XzCompanyProductMapper companyProductMapper;
    @Autowired
    private XzCompanySkillMapper companySkillMapper;
    @Autowired
    private XzFieldMapper fieldMapper;
    @Autowired
    private XzCompanyMienMapper companyMienMapper;


    @Override
    public PageBean<XzPostion> selPostionSendList(Map map, int page, int rows) {
        PageHelper.startPage(page, rows);
        List<XzPostion> plist = postionMapper.selPostionSendList(map);
        PageBean<XzPostion> pageBean = new PageBean<>(plist);
        return pageBean;
    }

    @Override
    public PageBean<XzPostion> selPostionIndex(int page, int rows, Map map) {
        PageHelper.startPage(page, rows);
        List<XzPostion> postion1 = postionMapper.selPostionList(map);

        PageBean<XzPostion> pp = new PageBean<>(postion1);
        List<Long> ll = new ArrayList<>();
        List<XzPostion> postions = pp.getList();
        for (XzPostion xzPostion : postions) {
            ll.add(xzPostion.getPostionId());
        }
        if (ll.size() > 0) {
            List<XzPostion> aa = postionMapper.selPostionByListId(ll);
            pp.setList(aa);
        }
        return pp;
        //return null;
    }

    @Override
    public PageBean<XzPostion> selPostionIndexx(int page, int rows, Map map) {
        PageHelper.startPage(page, rows);
        List<XzPostion> postionList = postionMapper.selPostionListx(map);
        postionList.forEach(postion -> {
            XzCompany company = companyMapper.selectByPrimaryKey(postion.getCompanyId());
            postion.setCompany(company);
            List<XzCompanyDomain> companyDomains = companyDomainMapper.selectByCompanyId(company.getCompanyId());
            company.setDomains(companyDomains);
        });
        return new PageBean<>(postionList);
    }

    @Override
    public PageBean<XzPostion> selectByCompanyId(int page, int rows, Long companyId) {
        PageHelper.startPage(page, rows);
        List<XzPostion> plist = postionMapper.selectByCompanyId(companyId);
        PageBean<XzPostion> pageBean = new PageBean<>(plist);
        return pageBean;
    }

    @Override
    public PageBean<XzPostion> selectByConditions(int page, int rows, XzPostion xzPostion) {
        PageHelper.startPage(page, rows);
        List<XzPostion> plist = postionMapper.selectByConditions(xzPostion);
        plist.forEach(postion123 -> {
            postion123.setCompany(companyMapper.selCompanyById(postion123.getCompanyId()));
        });
        PageBean<XzPostion> pageBean = new PageBean<>(plist);
        return pageBean;
    }

    @Override
    public List<XzPostion> selectByComId(Long companyId) {
        List<XzPostion> clist = postionMapper.selectByCompanyId(companyId);
        return clist;
    }

    @Override
    public List<XzPostion> selectCityByComId(Long companyId) {
        List<XzPostion> clist = postionMapper.selectCityByComId(companyId);
        return clist;
    }

    @Override
    public XzPostion selPostionInfo(Long postionId) {
        XzPostion xzPostion = postionMapper.selPostionInfo(postionId);
        return xzPostion;
    }

    @Override
    public PageBean<XzPostion> selInfoByName(int page, int rows, XzPostion xzPostion) {
        PageHelper.startPage(page, rows);
        List<XzPostion> plist = postionMapper.selInfoByName(xzPostion);
        PageBean<XzPostion> pageBean = new PageBean<>(plist);
        return pageBean;
    }

    @Override
    public PageBean<XzPostion> selInfoByComId(int page, int rows, XzPostion xzPostion) {
        PageHelper.startPage(page, rows);
        List<XzPostion> cplist = postionMapper.selInfoByComId(xzPostion);
        PageBean<XzPostion> pageBean = new PageBean<>(cplist);
        return pageBean;
    }

    @Override
    public int insertPostion(Map map) {
        String str1 = map.get("postionAgeMin").toString();
        String str2 = map.get("postionAgeMax").toString();
        String str3 = "-";
        String postionAge = str1 + str3 + str2;
        map.put("postionAge", postionAge);
        if (map.get("forceAgeMin") != null && map.get("forceAgeMax") != null) {
            String string1 = map.get("forceAgeMin").toString();
            String string2 = map.get("forceAgeMax").toString();
            String string3 = "-";
            String forceAge = string1 + string3 + string2;
            map.put("forceAge", forceAge);
        }

        int i = postionMapper.insertPostion(map);
        return i;
    }

    @Override
    public int updatePostion(Map map) {
        String str1 = map.get("postionAgeMin").toString();
        String str2 = map.get("postionAgeMax").toString();
        String str3 = "-";
        String postionAge = str1 + str3 + str2;
        map.put("postionAge", postionAge);
        if (map.get("forceAgeMin") != null && map.get("forceAgeMax") != null) {
            String string1 = map.get("forceAgeMin").toString();
            String string2 = map.get("forceAgeMax").toString();
            String string3 = "-";
            String forceAge = string1 + string3 + string2;
            map.put("forceAge", forceAge);
        }
        int i = postionMapper.updatePostion(map);
        return i;
    }

    @Override
    public int upPostion(XzPostion xzPostion, XzLogin xzLogin) {
        Map map = new HashMap();
        XzMsg xzMsg = new XzMsg();
        xzMsg.setMsgTital("您发布的职位" + xzPostion.getPostionName() + "审核未通过");
        xzMsg.setMsgContent(xzPostion.getFiled1());
        xzMsg.setMsgSendId(xzLogin.getLoginId());
        xzMsg.setMsgSendName("管理员");
        XzCompany xzCompany = companyMapper.selectByPrimaryKey(xzPostion.getCompanyId());
        xzMsg.setMsgReceiveId(xzCompany.getLoginId());
        xzMsg.setMsgReceiveName(xzCompany.getCompanyName());
        Date date = new Date();
        xzMsg.setMsgTime(date);
        xzMsg.setMsgType(5);
        xzMsg.setMsgIsread(0);
        xzMsg.setCreateTime(date);
        xzMsg.setOutId(xzLogin.getLoginId());
        System.out.println(xzLogin);
        msgMapper.insertSelective(xzMsg);
        int j = postionMapper.adoptPostion(xzPostion);
        return j;
    }


    //20180112-王继顺
    //修复数组越界错误，用函数式数据处理改写
    @Override
    public PageBean<XzPostion> selInfoByNameMob(int page, int rows, XzPostion xzPostion) {
        PageHelper.startPage(page, rows);
        List<XzPostion> plist = postionMapper.selInfoByNameMob(xzPostion);
        if (plist.size() == 0) {
            Map map = new HashMap();
            List<XzPostion> postionList = postionMapper.selPostionListx(map);
            postionList.stream().limit(3).forEach(plist::add);
        }

        plist.forEach(postion -> {
            long companyId = postion.getCompanyId();
            XzCompany xzCompany = companyMapper.selectByPrimaryKey(companyId);
            xzCompany.setWelfares(companyWelfareMapper.selectByCompanyId(companyId));
            xzCompany.setProducts(companyProductMapper.selectByCompanyId(companyId));
            xzCompany.setSkills(companySkillMapper.selectByCompanyId(companyId));
            List<XzCompanyDomain> domainList = companyDomainMapper.selectByCompanyId(companyId);
            xzCompany.setDomains(domainList);
            List<XzField> fieldList = new ArrayList<>();
            for (XzCompanyDomain domain : domainList) {
                fieldList.add(fieldMapper.selectByPrimaryKey(domain.getFieldId()));
            }
            xzCompany.setFields(fieldList);
            xzCompany.setPostions(postionMapper.selectByCompanyId(companyId));
            xzCompany.setMiens(companyMienMapper.selectByCompanyId(companyId));
            int i = xzCompany.getPostions().size();
            xzCompany.setPcount(i);
            postion.setCompany(xzCompany);
        });
        return new PageBean<>(plist);
    }

    //移动端公司ID查询职位
    @Override
    public PageBean<XzPostion> selByCompanyIdMob(int page, int rows, Long companyId) {
        PageHelper.startPage(page, rows);
        List<XzPostion> postionList = postionMapper.selByCompanyId(companyId);
        return new PageBean<>(postionList);
    }

    @Override
    public int deleteCollectMob(Map map) {
        int i = postionMapper.deleteCollectMob(map);
        return i;
    }

    @Override
    public PageBean<XzPostion> selPostionSendListMob(Long companyId, int page, int rows) {
        PageHelper.startPage(page, rows);
        Map<String, Long> map = new HashMap<>();
        map.put("companyId", companyId);
        List<XzPostion> plist = postionMapper.selCompanyPostionList(map);
        plist.forEach(postion -> {
            Long id = postion.getPostionId();
            int sees = postionSendMapper.selectStateCount(id, 0);
            int total = postionSendMapper.selectStateCount(id, 1);
            postion.setSees(sees);
            postion.setTotal(total);
        });
        return new PageBean<>(plist);
    }

    @Override
    public int insertCollect(Map map) {
        int i = postionMapper.insertCollect(map);
        return i;
    }

    @Override
    public int deleteCollect(Long collectId) {
        int i = postionMapper.deleteCollect(collectId);
        return i;
    }

    @Override
    public List<XzPostion> selectCollect(Long memberId) {
        List<XzPostion> list = postionMapper.selectCollect(memberId);
        return list;
    }

    @Override
    public int updateState(XzPostion xzPostion) {
        int i = postionMapper.updateState(xzPostion);
        return i;
    }

    @Override
    public List<XzPostion> selByCompanyId(Long companyId) {
        List<XzPostion> postionList = postionMapper.selByCompanyId(companyId);
        return postionList;
    }

    @Override
    public int deleteById(Long postionId) {
        postionSendMapper.delByPostionId(postionId);
        postionCollectMapper.deleteByPostionId(postionId);
        return postionMapper.deleteByPrimaryKey(postionId);
    }

    @Override
    public int delPostionAll(List<Long> postionIds) {
        for (Long postionId : postionIds) {
            postionSendMapper.delByPostionId(postionId);
            postionCollectMapper.deleteByPostionId(postionId);
        }
        return postionMapper.delPostionAll(postionIds);
    }

    @Override
    public XzPostion selectByPrimaryKey(Long postionId) {
        XzPostion xzPostion = postionMapper.selectByPrimaryKey(postionId);
        return xzPostion;
    }


    //以下为管理员用户操作的方法
    @Override
    public PageBean<XzPostion> selectAllByWelfare(int page, int rows) {
        PageHelper.startPage(page, rows);
        List<XzPostion> list = postionMapper.selectAllByWelfare();
        PageBean<XzPostion> pageBean = new PageBean<>(list);
        return pageBean;
    }

    @Override
    public PageBean<XzPostion> selectAll(int page, int rows) {
        PageHelper.startPage(page, rows);
        List<XzPostion> postionList = postionMapper.selectAll();
        PageBean<XzPostion> pageBean = new PageBean<>(postionList);
        return pageBean;
    }

    @Override
    public PageBean<XzPostion> selectAll_emp(int page, int rows, XzPostion xzPostion) {
        PageHelper.startPage(page, rows);
        List<XzPostion> postionList = postionMapper.selectAll_emp(xzPostion);
        PageBean<XzPostion> pageBean = new PageBean<>(postionList);
        return pageBean;
    }

    @Override
    public int updateByPrimaryKeySelective(XzPostion xzPostion) {
        int i = postionMapper.updateByPrimaryKeySelective(xzPostion);
        return i;
    }

    @Override
    public int adoptPostion(XzPostion xzPostion) {
        int i = postionMapper.adoptPostion(xzPostion);
        return i;
    }

    @Override
    public int selAdopt(Long postionId) {
        int i = postionMapper.selAdopt(postionId);
        return i;
    }


    @Override
    public int updateByWelfare(XzPostion xzPostion) {
        int i = postionMapper.updateByWelfare(xzPostion);
        return i;
    }
}
